package com.xjj.servicejob.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjj.entity.Job;
import com.xjj.entity.JobSubmit;
import com.xjj.servicejob.vo.JobSubmitDetailResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;


/**
 * @author sjy
 * @create 2022-10-20 19:24
 * 提交作业的mapper
 */
public interface JobSubmitMapper extends BaseMapper<JobSubmit> {
    /**
     * 根据学生id和作业id查询提交的作业
     *
     * @param studentId
     * @param jobId
     * @return
     */
    @Select("select id,status from xjj_job_submit where student_id = #{studentId} and job_id = #{jobId}")
    JobSubmit getByStudentIdAndJobId(@Param("studentId") Integer studentId, @Param("jobId") Integer jobId);

    /**
     * 学生提交作业时修改作业状态
     *
     * @param id
     */
    @Update("update xjj_job_submit set status = 'a' where id = #{id}")
    void add(Integer id);

    /**
     * 根据作业id查询所有提交的作业
     *
     * @param jobSubmit
     * @return
     */
    @SelectProvider(type = JobSubmitMapper.JobSubmitSqlProvider.class, method = "getByCondition")
    List<JobSubmit> getAllSubmittedJobSubmit(JobSubmit jobSubmit);

    @SelectProvider(type = JobSubmitMapper.JobSubmitSqlProvider.class, method = "getMyJob")
    @Results(
            {
                    @Result(column = "jid", property = "id", id = true),
                    @Result(column = "content", property = "content"),
                    @Result(column = "manager_id", property = "managerId"),
                    @Result(column = "klass_id", property = "klassId"),
                    @Result(column = "js", property = "status"),
                    @Result(column = "sid", property = "jobSubmit", one = @One(select = "com.xjj.servicejob.mapper.JobSubmitMapper.selectById"))
            }
    )
    List<Job> getByCondition(Job job);


    @Slf4j
    public static class JobSubmitSqlProvider {
        public static String getByCondition(JobSubmit jobSubmit) {
            String sql = new SQL() {
                {
                    SELECT("id", "status", "student_id", "level", "evaluation");
                    FROM("xjj_job_submit");
                    if (jobSubmit != null) {
                        if (jobSubmit.getStatus() != null && !jobSubmit.getStatus().equals("")) {
                            WHERE("status = #{status}");
                        }
                    }
                    WHERE("job_id = #{jobId}");
                }
            }.toString();
            System.out.println(sql);
            return sql;
        }

        public static String getMyJob(Job job) {
            String sql = new SQL() {
                {
                    SELECT("j.id jid", "manager_id", "j.status js ", "content", "klass_id", "s.status ss", "s.id sid", "s.job_id");
                    FROM("xjj_job j", "xjj_job_submit s");
                    if (job != null) {
                        if (job.getStatus() != null) {
                            WHERE("js = #{status}");
                        }
                        if (job.getKlassId() != null) {
                            WHERE("klass_id = #{klassId}");
                        }

                        if (job.getManagerId() != null) {
                            WHERE("manager_id=#{managerId}");
                        }
                        if (job.getSubmitStatus() != null) {
                            WHERE("s.status = #{submitStatus}");
                        }
                    }
                    WHERE("j.id = s.job_id");
                }
            }.toString();
            System.out.println(sql);
            return sql;
        }
    }
}
